@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

echo.
echo LimeReader Build Script
echo ============================================
echo.
echo Build Type: Release (Non-Interactive Mode)
set "CMAKE_BUILD_TYPE=Release"
echo [OK] Building Release version
echo.

:: 检查项目文件
if not exist "CMakeLists.txt" (
    echo [ERROR] 错误: 未找到 CMakeLists.txt 文件
    echo 请在项目根目录运行此脚本
    pause
    exit /b 1
)

:: 设置 Qt 路径 - 支持更多版本
set "QT_VERSIONS=6.9.2 6.8.1 6.8.0 6.7.0 6.6.0"
set "QT_PATH="

echo 正在搜索 Qt 安装...
for %%v in (%QT_VERSIONS%) do (
    if exist "C:\Qt\%%v\mingw_64" (
        set "QT_PATH=C:\Qt\%%v\mingw_64"
        echo [OK] 找到 Qt %%v 安装: !QT_PATH!
        goto :qt_found
    )
    if exist "C:\Qt\%%v\msvc2019_64" (
        set "QT_PATH=C:\Qt\%%v\msvc2019_64"
        echo [OK] 找到 Qt %%v MSVC 安装: !QT_PATH!
        goto :qt_found
    )
)

:qt_found
if "%QT_PATH%"=="" (
    echo [ERROR] 错误: 未找到 Qt 安装
    echo 请确保 Qt 安装在 C:\Qt\ 目录下
    echo 支持的版本: 6.6.0, 6.7.0, 6.8.0, 6.8.1, 6.9.2
    pause
    exit /b 1
)

:: 设置工具路径 - 支持多种可能的安装位置
set "CMAKE_PATH="
set "MINGW_PATH="

echo 检查构建工具...

:: 检查 CMake - 支持多种安装位置
if exist "C:\Qt\Tools\CMake_64\bin" (
    set "CMAKE_PATH=C:\Qt\Tools\CMake_64\bin"
    echo [OK] 找到 Qt CMake: !CMAKE_PATH!
) else if exist "C:\Program Files\CMake\bin" (
    set "CMAKE_PATH=C:\Program Files\CMake\bin"
    echo [OK] 找到系统 CMake: !CMAKE_PATH!
) else if exist "C:\Program Files (x86)\CMake\bin" (
    set "CMAKE_PATH=C:\Program Files (x86)\CMake\bin"
    echo [OK] 找到系统 CMake: !CMAKE_PATH!
) else (
    echo [WARN]  未找到 CMake，尝试使用系统 PATH
)

:: 检查 MinGW - 支持多种版本
for %%v in (1310 1120 810 730) do (
    if exist "C:\Qt\Tools\mingw%%v_64\bin" (
        set "MINGW_PATH=C:\Qt\Tools\mingw%%v_64\bin"
        echo [OK] 找到 Qt MinGW %%v: !MINGW_PATH!
        goto :mingw_found
    )
)

:: 检查系统 MinGW
if exist "C:\mingw64\bin" (
    set "MINGW_PATH=C:\mingw64\bin"
    echo [OK] 找到系统 MinGW: !MINGW_PATH!
) else (
    echo [WARN]  未找到 MinGW，尝试使用系统 PATH
)

:mingw_found

echo [OK] 构建工具检查通过

:: 检查 Poppler 库
if exist "mingw64-poppler\include\poppler\cpp\poppler-document.h" (
    echo [OK] 找到 Poppler 库，PDF 功能可用
) else (
    echo [WARN]  未找到 Poppler 库，PDF 功能将不可用
)

:: 检查 dynlibs 目录
if exist "dynlibs" (
    echo [OK] 找到项目动态库目录: dynlibs
    for /f %%i in ('dir /b dynlibs\*.dll 2^>nul ^| find /c /v ""') do (
        echo    包含 %%i 个动态链接库文件
    )
) else (
    echo [WARN]  未找到 dynlibs 目录，将跳过动态库复制
)

:: 设置环境变量
if not "%CMAKE_PATH%"=="" set "PATH=%CMAKE_PATH%;%PATH%"
if not "%MINGW_PATH%"=="" set "PATH=%MINGW_PATH%;%PATH%"

:: 设置Qt路径环境变量
set "QT_ROOT=%QT_PATH%"
set "CMAKE_PREFIX_PATH=%QT_PATH%"

:: 清理构建目录和CMake缓存
echo.
echo 清理构建目录和CMake缓存...
if exist "build" rmdir /s /q "build"
if exist "CMakeCache.txt" del /q "CMakeCache.txt"
if exist "CMakeFiles" rmdir /s /q "CMakeFiles"
if exist "cmake_install.cmake" del /q "cmake_install.cmake"
if exist "Makefile" del /q "Makefile"
if exist "build.ninja" del /q "build.ninja"

:: 创建构建目录
echo 创建构建目录...
mkdir "build"
cd "build"

:: 配置项目 - 启用 Poppler 支持
echo.
echo 配置 CMake 项目...

:: 首先尝试使用 MinGW Makefiles
cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE% -DUSE_POPPLER_LIBRARY=ON -DQT_ROOT="%QT_PATH%" -DCMAKE_PREFIX_PATH="%QT_PATH%" ..
if !errorlevel! neq 0 (
    echo [WARN]  MinGW Makefiles 配置失败，尝试不使用 Poppler 库...
    cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE% -DUSE_POPPLER_LIBRARY=OFF -DQT_ROOT="%QT_PATH%" -DCMAKE_PREFIX_PATH="%QT_PATH%" ..
    if !errorlevel! neq 0 (
        echo [WARN]  MinGW Makefiles 完全失败，尝试使用 Ninja...
        cmake -G "Ninja" -DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE% -DUSE_POPPLER_LIBRARY=ON -DQT_ROOT="%QT_PATH%" -DCMAKE_PREFIX_PATH="%QT_PATH%" ..
        if !errorlevel! neq 0 (
            echo [WARN]  Ninja 也失败，尝试使用默认生成器...
            cmake -DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE% -DUSE_POPPLER_LIBRARY=ON -DQT_ROOT="%QT_PATH%" -DCMAKE_PREFIX_PATH="%QT_PATH%" ..
            if !errorlevel! neq 0 (
                echo [ERROR] 所有 CMake 配置尝试都失败
                cd ..
                pause
                exit /b 1
            )
        )
    )
)

echo [OK] CMake 配置成功

:: 编译项目
echo.
echo 开始编译项目...

:: 检查是否有 Makefile
if exist "Makefile" (
    echo 使用 MinGW Make 编译...
    if not "%MINGW_PATH%"=="" (
        "%MINGW_PATH%\mingw32-make.exe" -j4
    ) else (
        mingw32-make.exe -j4
    )
    if !errorlevel! neq 0 (
        echo [ERROR] MinGW Make 编译失败
        cd ..
        pause
        exit /b 1
    )
) else if exist "build.ninja" (
    echo 使用 Ninja 编译...
    ninja
    if !errorlevel! neq 0 (
        echo [ERROR] Ninja 编译失败
        cd ..
        pause
        exit /b 1
    )
) else (
    echo 使用 CMake 构建...
    cmake --build . --config Release
    if !errorlevel! neq 0 (
        echo [ERROR] CMake 编译失败
        cd ..
        pause
        exit /b 1
    )
)

echo [OK] 编译成功完成

:: 创建目标目录结构
if not exist "bin" mkdir "bin"
if not exist "bin\Release" mkdir "bin\Release"

:: 检查生成的文件
if exist "LimeReader.exe" (
    echo.
    echo [OK] Executable generated successfully:
    echo    Path: %CD%\LimeReader.exe
    for %%f in ("LimeReader.exe") do echo    Size: %%~zf bytes

    :: 复制到 bin\Release 目录
    copy "LimeReader.exe" "bin\Release\LimeReader.exe" > nul
    echo [OK] Copied to bin\Release\LimeReader.exe

) else if exist "bin\LimeReader.exe" (
    echo.
    echo [OK] Executable found in bin directory:
    echo    Path: %CD%\bin\LimeReader.exe
    for %%f in ("bin\LimeReader.exe") do echo    Size: %%~zf 字节

    :: 移动到 bin\Release 目录
    copy "bin\LimeReader.exe" "bin\Release\LimeReader.exe" > nul
    echo [OK] Copied to bin\Release\LimeReader.exe

) else if exist "bin\Release\LimeReader.exe" (
    echo.
    echo [OK] Executable already in bin\Release directory:
    echo    Path: %CD%\bin\Release\LimeReader.exe
    for %%f in ("bin\Release\LimeReader.exe") do echo    Size: %%~zf bytes

) else (
    echo [ERROR] Error: Executable not found
    echo Checked: %CD%\LimeReader.exe
    echo Checked: %CD%\bin\LimeReader.exe
    echo Checked: %CD%\bin\Release\LimeReader.exe
    cd ..
    pause
    exit /b 1
)

:: 复制SSL库到bin\Release目录（解决libcurl依赖问题）
echo.
echo [SSL] Copying SSL libraries to resolve libcurl dependency...
set "ssl_copied=0"

:: 从MSYS2目录复制SSL库
if exist "C:\msys64\mingw64\bin\libssl-3.dll" (
    copy "C:\msys64\mingw64\bin\libssl-3.dll" "bin\Release\libssl-3.dll" > nul 2>&1
    if !errorlevel! equ 0 (
        echo [OK] Copied libssl-3.dll (MSYS2)
        set "ssl_copied=1"
    )
)

if exist "C:\msys64\mingw64\bin\libcrypto-3.dll" (
    copy "C:\msys64\mingw64\bin\libcrypto-3.dll" "bin\Release\libcrypto-3.dll" > nul 2>&1
    if !errorlevel! equ 0 (
        echo [OK] Copied libcrypto-3.dll (MSYS2)
        set "ssl_copied=1"
    )
)

:: 从Qt目录复制SSL库（备选方案）
if !ssl_copied! equ 0 (
    if exist "%QT_PATH%\..\Tools\mingw1310_64\bin\libssl-3.dll" (
        copy "%QT_PATH%\..\Tools\mingw1310_64\bin\libssl-3.dll" "bin\Release\libssl-3.dll" > nul 2>&1
        if !errorlevel! equ 0 (
            echo [OK] Copied libssl-3.dll (Qt Tools)
            set "ssl_copied=1"
        )
    )

    if exist "%QT_PATH%\..\Tools\mingw1310_64\bin\libcrypto-3.dll" (
        copy "%QT_PATH%\..\Tools\mingw1310_64\bin\libcrypto-3.dll" "bin\Release\libcrypto-3.dll" > nul 2>&1
        if !errorlevel! equ 0 (
            echo [OK] Copied libcrypto-3.dll (Qt Tools)
            set "ssl_copied=1"
        )
    )
)

:: 检查SSL库复制结果
if !ssl_copied! equ 1 (
    echo [OK] SSL libraries copied, libcurl dependency resolved
) else (
    echo [WARN] SSL libraries not found, may need to manually copy to build\bin\Release:
    echo        - libssl-3.dll
    echo        - libcrypto-3.dll
    echo    Possible sources:
    echo        - C:\msys64\mingw64\bin\
    echo        - C:\Qt\Tools\mingw1310_64\bin\
)

cd ..

:: 构建完成
echo.
echo ============================================
echo BUILD COMPLETED SUCCESSFULLY!
echo ============================================
echo Build Type: %CMAKE_BUILD_TYPE%
echo Output Directory: build\bin\Release
echo Executable: build\bin\Release\LimeReader.exe
echo.
echo To create installer, run: create_installer_simple.bat
echo.

:: 自动启动程序（可选）
if exist "build\bin\Release\LimeReader.exe" (
    echo Starting LimeReader...
    start "" "build\bin\Release\LimeReader.exe"
) else (
    echo [WARN] Executable not found at expected location
)
